package com.ruoyi.report_lqn.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_jl.domain.ReportJlXjlssjlbBaDetail;
import com.ruoyi.report_lqn.domain.ReportLqnLqcjlnfxjlbBaDetail;
import com.ruoyi.report_lqn.domain.vo.ReportLqnLqcjlnfxjlbBaDetailVo;
import com.ruoyi.report_lqn.mapper.ReportLqnLqcjlnfxjlbBaDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lqn.domain.bo.ReportLqnLqcjlnfxjlbBsInfoBo;
import com.ruoyi.report_lqn.domain.vo.ReportLqnLqcjlnfxjlbBsInfoVo;
import com.ruoyi.report_lqn.domain.ReportLqnLqcjlnfxjlbBsInfo;
import com.ruoyi.report_lqn.mapper.ReportLqnLqcjlnfxjlbBsInfoMapper;
import com.ruoyi.report_lqn.service.IReportLqnLqcjlnfxjlbBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青与粗集料的黏附性试验检测记录Service业务层处理
 *
 * @author tll
 * @date 2024-09-12
 */
@RequiredArgsConstructor
@Service
public class ReportLqnLqcjlnfxjlbBsInfoServiceImpl implements IReportLqnLqcjlnfxjlbBsInfoService {

    private final ReportLqnLqcjlnfxjlbBsInfoMapper baseMapper;

    private final ReportLqnLqcjlnfxjlbBaDetailMapper baDetailMapper;

    /**
     * 查询沥青与粗集料的黏附性试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportLqnLqcjlnfxjlbBsInfoVo queryById(Long excelId) {
        // 主表
        ReportLqnLqcjlnfxjlbBsInfoVo vo = baseMapper.selectVoById(excelId);
        // 子表
        LambdaQueryWrapper<ReportLqnLqcjlnfxjlbBaDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(excelId != null, ReportLqnLqcjlnfxjlbBaDetail::getExcelId, excelId);
        lqw.orderByAsc(ReportLqnLqcjlnfxjlbBaDetail::getCreateTime);
        List<ReportLqnLqcjlnfxjlbBaDetailVo> detailList = baDetailMapper.selectVoList(lqw);
        if (CollUtil.isNotEmpty(detailList)) {
            vo.setDetailList(detailList);
        }
        return vo;
    }

    /**
     * 查询沥青与粗集料的黏附性试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportLqnLqcjlnfxjlbBsInfoVo> queryPageList(ReportLqnLqcjlnfxjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqnLqcjlnfxjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportLqnLqcjlnfxjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青与粗集料的黏附性试验检测记录列表
     */
    @Override
    public List<ReportLqnLqcjlnfxjlbBsInfoVo> queryList(ReportLqnLqcjlnfxjlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportLqnLqcjlnfxjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqnLqcjlnfxjlbBsInfo> buildQueryWrapper(ReportLqnLqcjlnfxjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqnLqcjlnfxjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportLqnLqcjlnfxjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportLqnLqcjlnfxjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportLqnLqcjlnfxjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportLqnLqcjlnfxjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportLqnLqcjlnfxjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportLqnLqcjlnfxjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportLqnLqcjlnfxjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportLqnLqcjlnfxjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportLqnLqcjlnfxjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangpsj() != null, ReportLqnLqcjlnfxjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportLqnLqcjlnfxjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportLqnLqcjlnfxjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportLqnLqcjlnfxjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportLqnLqcjlnfxjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportLqnLqcjlnfxjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportLqnLqcjlnfxjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportLqnLqcjlnfxjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobmc()), ReportLqnLqcjlnfxjlbBsInfo::getBaobmc, bo.getBaobmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobbh()), ReportLqnLqcjlnfxjlbBsInfo::getBaobbh, bo.getBaobbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportLqnLqcjlnfxjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancr()), ReportLqnLqcjlnfxjlbBsInfo::getJiancr, bo.getJiancr());
        lqw.eq(StringUtils.isNotBlank(bo.getJilr()), ReportLqnLqcjlnfxjlbBsInfo::getJilr, bo.getJilr());
        lqw.eq(StringUtils.isNotBlank(bo.getFuhr()), ReportLqnLqcjlnfxjlbBsInfo::getFuhr, bo.getFuhr());
        lqw.eq(bo.getRiq() != null, ReportLqnLqcjlnfxjlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportLqnLqcjlnfxjlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportLqnLqcjlnfxjlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportLqnLqcjlnfxjlbBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getQuydd()), ReportLqnLqcjlnfxjlbBsInfo::getQuydd, bo.getQuydd());
        lqw.eq(StringUtils.isNotBlank(bo.getLiqcd()), ReportLqnLqcjlnfxjlbBsInfo::getLiqcd, bo.getLiqcd());
        lqw.eq(StringUtils.isNotBlank(bo.getLiqzlbh()), ReportLqnLqcjlnfxjlbBsInfo::getLiqzlbh, bo.getLiqzlbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJilcdmc()), ReportLqnLqcjlnfxjlbBsInfo::getJilcdmc, bo.getJilcdmc());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyff()), ReportLqnLqcjlnfxjlbBsInfo::getShiyff, bo.getShiyff());
        lqw.eq(StringUtils.isNotBlank(bo.getDaibsl()), ReportLqnLqcjlnfxjlbBsInfo::getDaibsl, bo.getDaibsl());
        return lqw;
    }

    /**
     * 新增沥青与粗集料的黏附性试验检测记录
     */
    @Override
    public Boolean insertByBo(ReportLqnLqcjlnfxjlbBsInfoBo bo) {
        ReportLqnLqcjlnfxjlbBsInfo add = BeanUtil.toBean(bo, ReportLqnLqcjlnfxjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改沥青与粗集料的黏附性试验检测记录
     */
    @Override
    public Boolean updateByBo(ReportLqnLqcjlnfxjlbBsInfoBo bo) {
        // 主表
        ReportLqnLqcjlnfxjlbBsInfo update = BeanUtil.toBean(bo, ReportLqnLqcjlnfxjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            baDetailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), ReportLqnLqcjlnfxjlbBaDetail.class));
        }
        return flag;
//        boolean upd = baseMapper.updateById(update) > 0;
//        // 子表
//        boolean upd2 = false;
//        boolean insert = false;
//        if (ObjectUtils.isNotEmpty(bo.getDetailList())) {
//            // 根据excelId查询明细
//            QueryWrapper<ReportLqnLqcjlnfxjlbBaDetail> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("excel_id", bo.getExcelId());
//            List<ReportLqnLqcjlnfxjlbBaDetail> list = baDetailMapper.selectList(queryWrapper);
//            for (ReportLqnLqcjlnfxjlbBaDetail det : bo.getDetailList()) {
//                if (ObjectUtils.isEmpty(list)) {
//                    // 新增
//                    insert = baDetailMapper.insert(det) > 0;
//                } else if (ObjectUtils.isNotEmpty(det.getDetailId())) {
//                    // 修改
//                    upd2 = baDetailMapper.updateById(det) > 0;
//                } else {
//                    throw new ServiceException("更新主键不能为空, 请刷新后重试!");
//                }
//            }
//        }
//        return upd && upd2 && insert;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqnLqcjlnfxjlbBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青与粗集料的黏附性试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
